
public class BinTree2<K extends KeyType<K>> {
	private int count;
	private K key;
	private BinTree2<K> left;
	private BinTree2<K> right;

	public BinTree2(K key) {
		this.count = 1;
		this.key = key;
		this.left = this.right = null;
	}

	/**
	 * inserts the given key into the binary tree
	 * 
	 * @param key
	 * @return true, if new node was created, false if a counter was incremented
	 */
	public synchronized boolean insert(K key) {
		// key equals this node's key
		if (key.equal(this.key))
		{
			count++;
			return false;
		}
		
		if (key.less(this.key)) { // key is smaller than this node's key
			if (left == null) { // left child is null
				left = new BinTree2<K>(key);
				return true;
			} else {
				return left.insert(key);
			}
		} else { // key is bigger than this node's key
			if (right == null) { // right child is null
				right = new BinTree2<K>(key);
				return true;
			} else {
				return right.insert(key);
			}
		}
	}
	
	
	/* TODO delete is not running !!!
	 * 
	 */
	public synchronized boolean delete(K key) {
		
		BinTree2<K> min = null;
		
		if (key.equal(this.key)) {
			count--;
			if (count <= 0) {
				//remove key
				if (left != null && right != null) {
					// node has 2 child nodes
					min = right.minimum();	// get minimum of right child
					this.key = min.key;		// copy data
					this.count = min.count;
					right.delete(min.key);
				} else {
					if (left != null)
						;
				}
			} else {
				return true;
			}
		}
		
		if (key.less(this.key)) {
			if (left == null) {
				return false;
			} else {
				return left.delete(key);
			}
		} else {
			if (right == null) {
				return false;
			} else {
				return right.delete(key);
			}
		}
	}
	
	private BinTree2<K> minimum() {
		if (left != null) 
			return left.minimum();
		else
			return this;
	}
	


}
